﻿// Inner Fire 游戏引擎库
// 索贝尔算子
//
// Copyright (c) 2024 by 尔西大帝. All rights reserved.
//
// 对输入纹理的最高MIP级别应用Sobel滤波器。
// 根据《DirectX12游戏开发实战》中的工具类修改。原作者：Frank Luna
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2024-12-04

#pragma once

#include <dx/core/dx_core.h>
#include <dx/texture/render_texture.h>

namespace ifire::dx {
class SobelFilter : public RenderTexture {
public:
  ///< summary>
  /// The width and height should match the dimensions of the input texture to
  /// apply the filter. Recreate when the screen is resized.
  ///</summary>
  SobelFilter(
      ID3D12Device* device, UINT width, UINT height, DXGI_FORMAT format);

  SobelFilter(const SobelFilter& rhs) = delete;
  SobelFilter& operator=(const SobelFilter& rhs) = delete;
  ~SobelFilter() = default;

  CD3DX12_GPU_DESCRIPTOR_HANDLE OutputSrv();

  UINT DescriptorCount() const;

  void BuildDescriptors(CD3DX12_CPU_DESCRIPTOR_HANDLE hCpuDescriptor,
      CD3DX12_GPU_DESCRIPTOR_HANDLE hGpuDescriptor, UINT descriptorSize);


  void Execute(ID3D12GraphicsCommandList* cmdList, ID3D12RootSignature* rootSig,
      ID3D12PipelineState* pso, CD3DX12_GPU_DESCRIPTOR_HANDLE input);

public:
  CD3DX12_CPU_DESCRIPTOR_HANDLE mhCpuSrv;
  CD3DX12_CPU_DESCRIPTOR_HANDLE mhCpuUav;

  CD3DX12_GPU_DESCRIPTOR_HANDLE mhGpuSrv;
  CD3DX12_GPU_DESCRIPTOR_HANDLE mhGpuUav;

protected:
  void BuildDescriptors() override;
  void BuildResource() override;

private:
  Microsoft::WRL::ComPtr<ID3D12Resource> mOutput = nullptr;
};

} // namespace ifire